const app = getApp();
var util = require('../../utils/util.js');
Page({
    data: {
        query: "",
        haveOpenId:false,
        updateTime: "",
        notes: [],
        features: [],
        userVisitCount: ""
    },


    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
     
        var token = wx.getStorageSync("token");
        console.log("token:::", token);
        if (!token) {
            app.loginReadyCallback = res => {
                wx.setStorageSync("token", res.data.token);
                this.setData({
                    haveOpenId:true
                });
            }
        }else{
            this.setData({
                haveOpenId: true
            });
        }

       

        //设置用户访问量
        if (app.globalData.userVisitCount && app.globalData.userVisitCount != null) {
            this.setData({
                userVisitCount: app.globalData.userVisitCount
            });
        } else {
            app.addUserVisitCallback = userVisitCount => {
                if (userVisitCount != null) {
                    this.setData({
                        userVisitCount: userVisitCount
                    });
                }
            };
        }

        this.getUpdateTime();

        this.getNotes();

    
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {},


    query: function() {

        var query = this.data.query;

        var errorMsg = this.checkQuery(query);
        if (errorMsg != '') {
            wx.showModal({
                title: '温馨提示',
                content: errorMsg,
                showCancel: false
            })
        } else {
            this.queryIndex(query);
        }
    },

    //校验参数
    checkQuery: function(query) {
        var errorMsg = "";
        if (query == "") {
            console.log("参数为空");
            errorMsg = "请输入姓名或摇号编号";
            return errorMsg;
        }
        return errorMsg;
    },

    closePopup: function() {
        this.setData({
            paramErrorVisible: false,
            errorMsg: ""
        })
    },

    onChange(e) {
        this.setData({
            query: e.detail.value
        })
    },

    //清空输入框
    onClear(e) {
        this.setData({
            query: ""
        })
    },

    goBatchQuery: function() {
        wx.navigateTo({
            url: '../batchQuery/batchQuery',
        })
    },

    getNotes: function() {
        wx.showLoading({
            title: '数据加载中',
        });

       

        //设置默认值
        var that = this;
        var postData = {
            type: "note"
        };
        var url = util.baseUrl + "/car/getNoticeList";
        console.log("url:", url);
        console.log("postData:", postData);
        wx.request({
            url: url,
            data: postData,
            method: 'GET',
            header: {
                // 'content-type': 'application/json',
            },
            success: function(res) {
                wx.hideLoading();
                //处理响应数据
                res = res.data;
                console.log("响应res:{}", res);
                if (res.code != 1) {
                    wx.showModal({
                        title: '温馨提示',
                        content: res.msg,
                        showCancel: false
                    });
                } else {
                    that.setData({
                        notes: res.data
                    });
                }

            },
            fail: function(e) {
                console.log("请求失败:", e);
                // that.popupMsg("服务器异常,请稍后再试");
            }
        })
    },

    getFeatures: function() {
        wx.showLoading({
            title: '数据加载中',
        });
        //设置默认值
        var that = this;
        var postData = {
            type: "feature"
        };
        var url = util.baseUrl + "/car/getNoticeList";
        console.log("url:", url);
        console.log("postData:", postData);
        wx.request({
            url: url,
            data: postData,
            method: 'GET',
            header: {
                // 'content-type': 'application/json',
            },
            success: function(res) {
                wx.hideLoading();
                //处理响应数据
                res = res.data;
                console.log("响应res:{}", res);
                if (res.code != 1) {
                    wx.showModal({
                        title: '温馨提示',
                        content: res.msg,
                        showCancel: false
                    });
                } else {
                    that.setData({
                        features: res.data
                    });
                }
            },
            fail: function(e) {
                console.log("请求失败:", e);
                // that.popupMsg("服务器异常,请稍后再试");
            }
        })
    },

    getUpdateTime: function() {
        wx.showLoading({
            title: '数据加载中',
        });
        //设置默认值
        var that = this;
        var postData = {
            type: "time"
        };
        var url = util.baseUrl + "/car/getNoticeList";
        console.log("url:", url);
        console.log("postData:", postData);
        wx.request({
            url: url,
            data: postData,
            method: 'GET',
            header: {
                // 'content-type': 'application/json',
            },
            success: function(res) {
                wx.hideLoading();
                //处理响应数据
                res = res.data;
                console.log("响应res:{}", res);
                if (res.code != 1) {
                    wx.showModal({
                        title: '温馨提示',
                        content: res.msg,
                        showCancel: false
                    });
                } else {
                    that.setData({
                        updateTime: res.data[0].content
                    });
                }
            },
            fail: function(e) {
                console.log("请求失败:", e);
                // that.popupMsg("服务器异常,请稍后再试");
            }
        })
    },

    queryIndex: function(query) {
        wx.showLoading({
            title: '查询中',
        })
        //设置默认值
        var that = this;
        var postData = {
            query: query
        };
        var url = util.baseUrl + "/car/queryIndex";
        console.log("url:", url);
        console.log("postData:", postData);
        wx.request({
            url: url,
            data: postData,
            method: 'GET',
            header: {
                // 'content-type': 'application/json',
            },
            success: function(res) {
                wx.hideLoading();
                //处理响应数据
                that.parseRes(res.data);
            },
            fail: function(e) {
                console.log("请求失败:", e);
                wx.showModal({
                    title: '温馨提示',
                    content: '服务器异常,请稍后再试',
                    showCancel: false
                })
            }
        })
    },

    parseRes: function(res) {
        console.log("响应:", res);
        if (res.code != 1) {
            //显示msg
            console.log("请求出错,错误原因:", res.msg);
            wx.showModal({
                title: '温馨提示',
                content: res.msg,
                showCancel: false
            });
        } else {
            //判断数组是否为空
            if (res.data.length == 0) {
                console.log("数组为空");
                wx.navigateTo({
                    url: '../carQueryFail/carQueryFail'
                })
            } else if (res.data.length == 1) {
                console.log("data:", res.data);
                let indexList = JSON.stringify(res.data);
                wx.navigateTo({
                    url: '../carQuerySuccessOnly/carQuerySuccessOnly?indexList=' + escape(indexList),
                })
            } else {
                console.log("data:", res.data);
                let indexList = JSON.stringify(res.data);
                wx.navigateTo({
                    url: '../carQuerySuccess/carQuerySuccess?indexList=' + escape(indexList),
                })
            }
        }
    },

    popupMsg: function(msg) {
        this.setData({
            paramErrorVisible: true,
            errorMsg: msg
        })
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function(ops) {
        var date = new Date();
        if (date.getDate()>=26){
            var month = date.getMonth()+1+"月";
        }else{
            var month = date.getMonth()+"月";
        }
        var year = date.getFullYear()+"年";
        var title = year+month+"杭州市小客车摇号结果出来啦,快来查查吧";
        
        if (ops.from === 'button') {
            // 来自页面内转发按钮
            console.log(ops.target)

        }
        return {
            title: title,
            path: '/pages/carQuery/carQuery',
            success: function(res) {
                // 转发成功
                console.log("转发成功:" + JSON.stringify(res));
            },
            fail: function(res) {
                // 转发失败
                console.log("转发失败:" + JSON.stringify(res));
            }
        }
    }
})